function log(data){
    if(window.console)
        console.log(data);
};

function show(obj,visible,duration,callback){
    if(visible)
        if($.support.opacity) {
            obj.stop().fadeIn(duration,callback);
        }
        else{
            obj.show();
            if(callback) callback();
        }
    else
    if($.support.opacity) {
        obj.stop().fadeOut(duration,callback);
    }
    else{
        obj.hide();
        if(callback) callback();
    }
}

function merge(from, to){
    for(var i in from){
        if(from.hasOwnProperty(i)){
            to[i] = from[i];
        }
    }
}


$(document).ready(function(){

    var Timer = (function(){

        var clockParent = $("#left-test-time-wrapper > div"),
            ico = $("#left-test-time-wrapper > span:eq(0)")


        var Unit = defineClass(function(time, radix, countDown, lastUnit){

            if(time >= radix || time < 0){
                throw "超出范围";
            }

            this.time = time;
            this.countDown = countDown;
            this.current = countDown ? time : 0;
            this.radix = radix;
            this.htmlElements = [];

            for(var i = 0, length = (radix + "").length; i < length; i++){
                var element = $("<span class='digit'></span>");
                this.htmlElements.push(element);
                clockParent.append(element);
            }

            if(!lastUnit) clockParent.append($("<span class='colon'></span>"));
            this.refreshStyle();

        }, null, {
            htmlElements: null,
            time: 0,
            radix: 60,
            current: 0,
            countDown: false,
            prevUnit: null,
            refreshStyle: function(){

                var digitStr = this.current + "",
                    digitStr2 = this.radix + "";

                while(digitStr.length < digitStr2.length){
                    digitStr = "0" + digitStr;
                }

                for(var i = 0, length = (this.radix + "").length; i < length; i++){
                    this.htmlElements[i].removeClass().addClass("digit digit" + digitStr.charAt(i));
                }

            },
            step: function(){                          /*改~~~~~~~~~~~~~~~~~*/
                if(this.countDown){
                    if(this.current == 0){
                        if(!(this.prev && this.prev.step())){
                            return false;
                        }
                        this.time = this.radix;
                        this.current = this.radix;
                    }
                    this.current -= 1;
                    this.refreshStyle();
                    return true;
                }

                if(this.current + 1 == this.radix){
                    if(!(this.prev && this.prev.step())){
                        return false;
                    }
                    this.time = 0;
                }
                this.current += 1;
                this.refreshStyle();
                return true;

                // this.current += 1 + -2 * this.countDown;

            }
        });


        var Timer = defineClass(function(time, countDown, duration, callback){

            if(time > 86399 || time < 1) throw "超出范围";

            var 时 = new Unit(Math.floor(time/3600), 24, countDown);

            time = time - Math.floor(time/3600) * 3600

            var 分 = new Unit(Math.floor(time/60), 60, countDown);

            time = time - Math.floor(time/60) * 60;

            var 秒 = new Unit(time, 60, countDown, true);

            /*var 毫秒 = new Unit(98, 99, countDown, true);

             毫秒.prev = 秒;*/
            秒.prev = 分;
            分.prev = 时;

            var icoInterval = setInterval(function(){
                if(ico.is(":visible")) show(ico, false, 300);
                else show(ico, true, 300);
            }, 1000);

            var interval = setInterval(function(){
                if(!秒.step()){
                    clearInterval(interval);
                    clearInterval(icoInterval);
                    show(ico, true, 300);
                    callback();
                }
            }, duration);

        }, null, {
            start: function(){

            },
            pause: function(){

            },
            reset: function(){

            },
            stop: function(){

            }
        });

        return Timer;

    })();

    //true 倒计时
    //restSecond 倒计时总时间，是使用页面定义
    var timer = new Timer(restSecond, true, 1000, callBackFun);

});

function defineClass(constructor, parent, properties, statics, isSingleton){
    if(isSingleton){
        var instance, oldConstructor = constructor;
        constructor = function(){
            if(instance) return instance;
            oldConstructor.call(this);
            instance = this;
        }
    }
    constructor.prototype = parent ? new parent : {};
    merge(properties, constructor.prototype);
    merge(statics, constructor);
    constructor.prototype.constructor = constructor;
    return constructor;
}